package org.iweb.util;

import java.lang.reflect.InvocationTargetException;

@SuppressWarnings({"rawtypes","unchecked"})
public class BeanUtil{

	public static Object invokeByClass(Class clazz,String methodName,Object... parameters) throws Exception{		
		Class[] argumentTypes=new Class[parameters.length];
		
		for(int i=0;i<parameters.length;i++){
			argumentTypes[i]=parameters[i].getClass();
		}
		
		try {
			return clazz.getMethod(methodName, argumentTypes).invoke(clazz.newInstance(), parameters);
		} catch (IllegalAccessException | IllegalArgumentException
				| InvocationTargetException | NoSuchMethodException
				| SecurityException | InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new Exception("Impossibile invocare il metodo sull'oggetto");
		}	
	}
	
	public static Object invokeByObj(Object obj,String methodName,Object... parameters) throws Exception{		
		Class[] argumentTypes=new Class[parameters.length];
		
		for(int i=0;i<parameters.length;i++){
			argumentTypes[i]=parameters[i].getClass();
		}
		
		try {
			return obj.getClass().getMethod(methodName, argumentTypes).invoke(obj, parameters);
		} catch (IllegalAccessException | IllegalArgumentException
				| InvocationTargetException | NoSuchMethodException
				| SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new Exception("Impossibile invocare il metodo sull'oggetto");
		}	
	}
	
}
